package cn.com.twoke.game.client.ui;

import cn.com.twoke.game.client.GameClientManager;
import cn.com.twoke.game.client.cache.ClientCacheManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public abstract class BaseUI extends JFrame implements WindowListener {

    public BaseUI(String title) throws HeadlessException {
        super(title);
        JPanel content = initContent();
        if (null != content) {
            this.add(content, BorderLayout.CENTER);
        }
        this.addWindowListener(this);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    public JPanel initContent() {
        return null;
    }


    public void open() {
        setVisible(true);
    }

    public void showError(String message) {
        JOptionPane.showMessageDialog(this,message,"消息提示",JOptionPane.WARNING_MESSAGE);
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        if (ClientCacheManager.INSTANCE.isLogin()) {
            int userOption = JOptionPane.showConfirmDialog(this,
                    "确认退出登录吗？","提示", JOptionPane.YES_NO_OPTION);
            if (userOption == JOptionPane.YES_OPTION) {
                GameClientManager.INSTANCE.logout(ClientCacheManager.INSTANCE.getUsername());
            }
        } else {
            System.exit(0);
        }
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
